package com.gzy.network.api

import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * @program: net-app
 * @description: 访问服务器API接口
 * @author : guzy
 * @date : 2022-04-18 14:22
 **/
class APIClient {
  private object Holder {
    val INSTANCE = APIClient()
  }

  companion object {
    val instance = Holder.INSTANCE
  }

  fun <T> instanceRetrofit(apiInterface: Class<T>): T {

    val mOkHttpClient = OkHttpClient().newBuilder().apply {
      readTimeout(10000, TimeUnit.SECONDS) //添加读区超时时间
      connectTimeout(10000, TimeUnit.SECONDS)//添加连接超时时间
      writeTimeout(10000, TimeUnit.SECONDS)//添加写出超时时间
    }.build()

    val retrofit: Retrofit = Retrofit.Builder()
      //根路径
      .baseUrl("")
      //请求方
      .client(mOkHttpClient)
      //响应方
      //response的事情  回来
      .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
      .addConverterFactory(GsonConverterFactory.create())
      .build()

    return retrofit.create(apiInterface)
  }
}